#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time   : 2021/7/16 0:44
# @Author : cjw
import threading
import time

from typing import Optional
from faker import Factory


class GenerateData:
    """数据生成"""

    def __init__(self, locale: str = 'zh_CN'):
        self.faker = Factory.create(locale)

    def name(self, num: Optional[int] = None):
        """
        姓名
        :param num: 生成数量
        :return:
        """
        if num:
            name = [self.faker.name() for _ in range(num)]
        else:
            name = self.faker.name()
        return name

    def phone(self, num: Optional[int] = None):
        """
        手机号
        :param num:
        :return:
        """
        if num:
            phone = [self.faker.phone_number() for _ in range(num)]
        else:
            phone = self.faker.phone_number()
        return phone

    def address(self, num: Optional[int] = None):
        """
        地址
        :param num:生成数量
        :return:
        """
        if num:
            address = [self.faker.address() for _ in range(num)]
        else:
            address = self.faker.address()
        return address

    def id_card(self, num: Optional[int] = None, min_age: int = 18,
                max_age: int = 90, gender: Optional[str] = None):
        """
        身份证：可以指定生成年龄段
        :param num: 数量
        :param min_age: 最小年龄
        :param max_age: 最大年龄
        :param gender: 性别，男性：M/m，女性：F/f, 不指定随机生成性别的身份证
        :return:
        """
        if num:
            id_card = [self.faker.ssn(min_age, max_age, gender) for _ in range(num)]
        else:
            id_card = self.faker.ssn(min_age, max_age, gender)
        return id_card

    def email(self, num: Optional[int] = None, domain: Optional[str] = None):
        """
        邮箱
        :param num: 生成数量
        :param domain: 邮箱域名：例如’qq.com‘
        :return:
        """
        if num:
            email = [self.faker.email(domain) for _ in range(num)]
        else:
            email = self.faker.email(domain)
        return email

    def word(self, is_words: bool = False, nb=3, ext_word_list=None, unique=False):
        """
        文字词组
        :param is_words: 是否生成多个
        :param nb: 生成多个词组的数量，默认是3
        :param ext_word_list: 生成额外的词组列表
        :param unique: 是否唯一
        :return:
        """
        if is_words:
            return self.faker.words(nb, ext_word_list, unique)
        else:
            return self.faker.word(ext_word_list)

    @classmethod
    def unique_name(cls):
        """
        获取唯一名称
        :return:
        """
        return f'测试名称{threading.currentThread().name}{str(round(time.time() * 1000))}'
